package com.xiaoyi.encrypt;

import com.xiaoyi.annotataion.Encrypt;
import com.xiaoyi.config.EncryptionConfig;
import com.xiaoyi.util.AesUtil;

import java.util.Objects;

/**
 * aes 对称加密 能加能解密
 *
 * @author xiaoyi
 * @version 1.0.0
 * @date 2023/08/01 15:17:00
 */
public class AesEncryptor implements IEncryptorStrategy {

    @Override
    public String encrypt(String value, EncryptionConfig config, Encrypt encrypt) {
        return AesUtil.encrypt(value, getPassword(config, encrypt));
    }

    @Override
    public String decrypt(String value, EncryptionConfig config, Encrypt encrypt) {
        return AesUtil.decrypt(value, getPassword(config, encrypt));
    }

    private String getPassword(EncryptionConfig config, Encrypt annotation) {
        if (Objects.nonNull(annotation.password()) && !"".equals(annotation.password())) {
            return annotation.password();
        }
        return config.getPassword();
    }
}
